<?php

header("Access-Control-Allow-Origin:*");
/*
  版本1
  学生投票接口
  接口方式:http post
  接口:setVote.php
  传入数据:{"userId":"23","userName":"123","choice":"1"}

  版本2
  1.学生投票接口
  接口方式:http post
  接口:setVote.php
  传入数据:{"userId":"23","userName":"123","choice":"3","voteTime":"1234566723"}
  测试地址:http://192.168.16.115/openapi/om_html5/setVote.php?userId=23&userName=张三&choice=3&voteTime=1234566723
 */
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
@error_reporting(0);
@set_time_limit(0);
@set_magic_quotes_runtime(0);
@ini_set('memory_limit', '512M');
$whiteboard_config_array = array();
//ini_set("display_errors", "On");
//error_reporting(E_ALL);
//DEBUG 接收投票数据
$userId = isset($_REQUEST['userId']) ? $_REQUEST['userId'] : '';
$userName = isset($_REQUEST['userName']) ? $_REQUEST['userName'] : '';
$choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : '';
$voteTime = isset($_REQUEST['voteTime']) ? $_REQUEST['voteTime'] : '';
$roomId = isset($_REQUEST['roomId']) ? $_REQUEST['roomId'] : '';
if ($userId && $userName && $choice !='') {
    //DEBUG 解析白板配置文件
    $isp = isset($_REQUEST['isp']) && !empty($_REQUEST['isp']) ? $_REQUEST['isp'] : ''; //运营商编号 1 电信 2 移动 3 联通
    if (empty($isp)) {
        $isp = 1;
    }
    $whiteboard_config_array = parse_ini_file("../../config/room/config_whiteboard_" . $isp . ".ini", false);
    require '../../source/class/memory/memory_driver_redis_multi.php';

    $config_redis = array();
    $config_redis['master'] = array(
        array('host' => $whiteboard_config_array['MEMORY_REDIS_MASTER_SERVER'],
            'port' => $whiteboard_config_array['MEMORY_REDIS_MASTER_PORT'],
            'auth' => $whiteboard_config_array['MEMORY_REDIS_MASTER_AUTH'],
            'db' => $whiteboard_config_array['MEMORY_REDIS_MASTER_DB'],
            'pconnect' => $whiteboard_config_array['MEMORY_REDIS_MASTER_PCONNECT'],
            'timeout' => $whiteboard_config_array['MEMORY_REDIS_MASTER_TIMEOUT']
        )
    );
    $config_redis['slave'] = array(
        array('host' => $whiteboard_config_array['MEMORY_REDIS_SLAVE_SERVER'],
            'port' => $whiteboard_config_array['MEMORY_REDIS_MASTER_PORT'],
            'auth' => $whiteboard_config_array['MEMORY_REDIS_MASTER_AUTH'],
            'db' => $whiteboard_config_array['MEMORY_REDIS_MASTER_DB'],
            'pconnect' => $whiteboard_config_array['MEMORY_REDIS_MASTER_PCONNECT'],
            'timeout' => $whiteboard_config_array['MEMORY_REDIS_MASTER_TIMEOUT']
        )
    );
    @file_put_contents('./logs/vote/vote_data_' . $voteTime . '.log', $userId . '_' . $userName . '_' . serialize($choice) . '_' . date('Y-m-d H:i:s', time()) . PHP_EOL, FILE_APPEND);
    //DEBUG 初始化redis
    $redis = RedisClient::getInstance($config_redis);
    if(is_array($choice)){
        foreach ($choice AS $key => $value) {
            $redis->incr('vote_num_' . $voteTime . '_' . $value);
        }  
    }else{
        $redis->incr('vote_num_' . $voteTime . '_' . $choice);
    }
    echo 'ok';
    $redis->close();
}
?>